<?php

class PtrtinraoController extends Zend_Controller_Action {

	//Mang tham so nhan duoc o moi Action
	protected $_arrParam;

	//Duong dan cua Controller
	protected $_currentController;

	//Duong dan cua Action chinh
	protected $_actionMain;

	//Thong so phan trang
	protected $_paginator = array('itemCountPerPage' => 10, 'pageRange' => 3);
	protected $_namespace;
	
	public function init() {
		$file_uri = $this->view->baseUrl(DEFAULT_PATH . '/js/jquery.dd.js');
	
		// Meta
		$this->view->headMeta()->setName('description', 'Trang web chuyên về thông tin phòng trọ.');
		$this->view->headMeta()->appendName('keywords', 'phòng trọ, phòng cho thuê, thuê phòng, nhà trọ');

		//Mang tham so nhan duoc o moi Action
		$this->_arrParam = $this->_request->getParams();

		//Duong dan cua Controller
		$this->_currentController = '/' . $this->_arrParam['module'] . '/' . $this->_arrParam['controller'];

		//Duong dan cua Action chinh
		$this->_actionMain = '/' . $this->_arrParam['module'] . '/' . $this->_arrParam['controller'] . '/index';

		$this->_paginator['currentPage'] = $this->_request->getParam('page', 1);
		$this->_arrParam['paginator'] = $this->_paginator;

		//Luu cac du lieu filter vao SESSION
		//Dat ten SESSION
		$this->_namespace = $this->_arrParam['module'] . '-' . $this->_arrParam['controller'];
		
		//Truyen ra view
		$this->view->arrParam = $this->_arrParam;
		$this->view->currentController = $this->_currentController;
		$this->view->actionMain = $this->_actionMain;
		
		// Get items tin mới của table Ptr_Tin_Rao
		$model = new Default_Model_PtrTinRaoMapper();
		$items = $model->fetch($this->_arrParam, 'trang_thai = 1', 5, array(array('colName' => 'ngay_dang', 'order' => 'DESC')), 'list');
		
		$this->view->itemsNewPtrTinTro = $items;
		
		// Get items tin hot của table Ptr_Tin_Rao
		$model = new Default_Model_PtrTinRaoMapper();
		$items = $model->fetch($this->_arrParam, 'trang_thai = 1', 5, array(array('colName' => 'luot_xem', 'order' => 'DESC')), 'list');
		
		$this->view->itemsHotPtrTinTro = $items;
		
		// Get items tin nổi bật của table Tin_Tuc
		$model = new Default_Model_TinTucMapper();
		$items = $model->fetchBy('trang_thai = 1', array(array('colName' => 'luot_xem', 'order' => 'DESC')), 1, 5, 'list');
		
		$this->view->itemsHotTinTuc = $items;
		
		$model = new Default_Model_ThongTinCtyMapper();
		$thong_tin_cty = $model->fetchAll();
		$this->view->thong_tin_cty = $thong_tin_cty;
		
		$form = new Default_Form_SendMail();
		$this->view->form_sendmailtoposter = $form;

		// Kiểm tra đăng nhập
		$state_login = false;
		$auth = Zend_Auth::getInstance()->setStorage(new Zend_Auth_Storage_Session('user'));
		if($auth->hasIdentity()){
			$state_login = true;
		}
		$this->view->state_login = $state_login;
		
		// Quảng cáo ở trang tin rao {
		$adv_page = 3;
		$position1 = 1;
		$position2 = 2;
		$position4 = 4;
		$position5 = 5;
		$position6 = 6;
		
		$model = new Default_Model_AdvertisementMapper();
		$this->view->adv1 = $model->fetchBy($adv_page, $position1); // Quảng cáo ở banner gần logo
		$this->view->adv2 = $model->fetchBy($adv_page, $position2); // Quảng cáo ở bên trái
		$this->view->adv4 = $model->fetchBy($adv_page, $position4); // Quảng cáo ở bên phải
		$this->view->adv5 = $model->fetchBy($adv_page, $position5); // Logo ở bên trái
		$this->view->adv6 = $model->fetchBy($adv_page, $position6); // Logo ở bên phải
		
		$model = new Default_Model_PointMapper();
		$arrData = $model->fetchAll('page_id=' . $adv_page, 'view');
		
		While (list($key, $val) = each($arrData)) {
			switch($val->getPosition_id()) {
				case '1':
					$this->view->point1 = $val->getPoint();
					break;
				case '2':
					$this->view->point2 = $val->getPoint();
					break;
				case '3':
					$this->view->point3 = $val->getPoint();
					break;
				case '4':
					$this->view->point4 = $val->getPoint();
					break;
				case '5':
					$this->view->point5 = $val->getPoint();
					break;
				case '6':
					$this->view->point6 = $val->getPoint();
					break;
			}
		}
		// }
	}
	
	public function indexAction() {
		$loai_tin = 2;
		if (isset($this->_arrParam['loaitin'])) {
			$loai_tin = $this->_arrParam['loaitin'];
		}
		
		switch ($loai_tin) {
			case 1:
				$title = "Tìm phòng trọ";
			break;
			
			default:
				$title = "Phòng cho thuê";
			break;
		}
		
		// Get items tin nổi bật của table PtrTinRao
		$model = new Default_Model_PtrTinRaoMapper();
		$order = array(array('colName' => 'goi_tin', 'order' => 'ASC'), array('colName' => 'ngay_dang', 'order' => 'DESC'));
		$items = $model->fetch($this->_arrParam, 'loai_tin = ' . $loai_tin . ' AND trang_thai = 1', 0, $order, 'list');
		
		$this->view->items = $items;
		
		// Đếm số item trong tb_ptr_tin_rao
		$totalItem = $model->countItem(null, 'loai_tin = ' . $loai_tin . ' AND trang_thai = 1');
		$this->view->totalItem = $totalItem;
		
		$paginator = new Zendhlt_Paginator();
		$this->view->panigator = $paginator->createPaginator($totalItem, $this->_paginator);
		
		$this->view->title = $title;
	}

	// Xem chi tiết tin
	public function viewAction() {
		
		$this->view->headScript()->appendFile('https://maps.googleapis.com/maps/api/js?key=AIzaSyDypXt_xTgAMOTpSEdwmPXjUA0JGGbyXhY&sensor=false');
		$file_uri = $this->view->baseUrl(PUBLIC_PATH . '/js/google.map.js');
		$this->view->headScript()->appendFile($file_uri);
		
		// Get view chi tiết của table Ptr_Tin_Rao
		$model = new Default_Model_PtrTinRaoMapper();
		$data = array('luot_xem' => new Zend_Db_Expr('luot_xem + 1'));
		$model->change($this->_arrParam['id'], $data);
		$item = $model->fetch($this->_arrParam, 'trang_thai = 1 AND id = ' . $this->_arrParam["id"]);
		
		$this->view->bodyScripts = 'onload="displayLocation(\'' . $item->dia_chi . '\', \'' . $item->latitude . '\',\'' . $item->longitude .'\')"';
		
		$this->view->itemsViewPtrTinTro = $item;
		$address = $item->getDia_chi();
		$lat=$item->getLatitude();
		$lng=$item->getLongitude();
		
		// Title
		$this->view->headTitle($item->tieu_de);
		
		$form = new Default_Form_SendForFriend();
		$form->link_bai_viet->setValue("ptrtinrao/view/id/" . $this->_arrParam["id"]);
		$this->view->form_sendmail = $form;
	}
	
	// Báo xấu tin
	public function baoxauAction() {
		$model = new Default_Model_PtrTinRaoMapper();
		$data = array('bao_xau' => new Zend_Db_Expr('bao_xau + 1'));
		$model->change($this->_arrParam['id'], $data);
		
		$this->_redirect('/ptrtinrao/view/id/' . $this->_arrParam['id']);
	}
}